package com.tykboot.controller;

import com.tykboot.core.base.BaseController;
import com.tykboot.core.persistence.Criteria;
import com.tykboot.core.persistence.Restrictions;
import com.tykboot.entity.TbBalance;
import com.tykboot.entity.TbCreditLine;
import com.tykboot.message.ResponseMsg;
import com.tykboot.service.ITbBalanceService;
import com.tykboot.service.ITbCreditLineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Balance Controller
 *
 * @author LiuYu
 */
@Api(tags = "balance", description = "/api/balances")
@RestController
@RequestMapping(BaseController.API + "/balances")
public class TbBalanceController extends BaseController {

    @Autowired
    private ITbBalanceService tbBalanceService;

    @Autowired
    private ITbCreditLineService tbCreditLineService;

    @ApiOperation(value = "Get accounts", notes = "Get accounts")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "financialId", value = "financialId", dataType = "string", paramType = "header", required = true)
    })
    @GetMapping("")
    public ResponseMsg<List<TbBalance>> queryBalances(@RequestHeader("financialId") String financialId) {
        ResponseMsg<List<TbBalance>> responseMsg = new ResponseMsg<>();
        Criteria<TbBalance> req = new Criteria<>();
        req.add(Restrictions.eq("financialId", financialId));
        List<TbBalance> tbBalances = tbBalanceService.findAll(req);
        if (tbBalances != null && !tbBalances.isEmpty()) {
            for (TbBalance tbBalance : tbBalances) {
                Criteria<TbCreditLine> creditLineCriteria = new Criteria<>();
                creditLineCriteria.add(Restrictions.eq("accountId", tbBalance.getAccountId()));
                creditLineCriteria.add(Restrictions.eq("financialId", financialId));
                tbBalance.setCreditLine(tbCreditLineService.findAll(creditLineCriteria));
            }
            responseMsg.setData(tbBalances);
        }
        return responseMsg;
    }

    @ApiOperation(value = "Get account Balances", notes = "Get Balances related to an account")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "financialId", value = "financialId", dataType = "string", paramType = "header", required = true),
            @ApiImplicitParam(name = "accountId", value = "accountId", dataType = "string", paramType = "path", required = true)
    })
    @GetMapping("/{accountId}")
    public ResponseMsg<List<TbBalance>> queryBalancesById(@RequestHeader("financialId") String financialId,
                                                          @PathVariable("accountId") String accountId) {
        ResponseMsg<List<TbBalance>> responseMsg = new ResponseMsg<>();
        Criteria<TbBalance> req = new Criteria<>();
        req.add(Restrictions.eq("accountId", accountId));
        req.add(Restrictions.eq("financialId", financialId));
        List<TbBalance> tbBalances = tbBalanceService.findAll(req);
        if (tbBalances != null && !tbBalances.isEmpty()) {
            for (TbBalance tbBalance : tbBalances) {
                Criteria<TbCreditLine> creditLineCriteria = new Criteria<>();
                creditLineCriteria.add(Restrictions.eq("accountId", accountId));
                creditLineCriteria.add(Restrictions.eq("financialId", financialId));
                tbBalance.setCreditLine(tbCreditLineService.findAll(creditLineCriteria));
            }
            responseMsg.setData(tbBalances);
        }
        return responseMsg;
    }

}
